package org.jypj.dev.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.jypj.dev.dao.*;
import org.jypj.dev.vo.indexVo.IRoad;
import org.jypj.dev.vo.indexVo.ITrainUserWork;
import org.jypj.dev.service.IIndexService;
import org.jypj.dev.vo.SearchVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("indexService")
public class IndexServiceImpl implements IIndexService {


    @Resource
    private IIndexDao indexDao;

    @Override
    public List<IRoad> selectAllRoads(SearchVo searchVo) {
        List<IRoad> iRoads = indexDao.selectAllRoads(searchVo);
        for (IRoad iRoad : iRoads) {
            Gson gson = new Gson();
            List<ITrainUserWork> trainUserWorks = gson.fromJson(iRoad.getTrainWorkJson(),
                    new TypeToken<List<ITrainUserWork>>() {
                    }.getType());
            iRoad.setTrainUserWorkList(trainUserWorks);
            List<ITrainUserWork> trainUserWorkList = iRoad.getTrainUserWorkList();
            for (ITrainUserWork iTrainUserWork : trainUserWorkList) {
                iTrainUserWork.getUser().setLoginId(iRoad.getId());
                iTrainUserWork.getUsers().add(iTrainUserWork.getUser());
            }
        }
        return iRoads;
    }
}
